package com.englisheveryday.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.englisheveryday.constants.Constants;
import com.englisheveryday.db.ArticleDAO;
import com.englisheveryday.db.CategoryDAO;
import com.englisheveryday.net.DefaultConnection;
import com.englisheveryday.utils.L;
import com.englisheveryday.vo.ArticleVO;
import com.englisheveryday.vo.CategoryVO;


/**
 * <pre>
 *    
 * </pre>
 * @author 马永华
 * @date 2012-9-16 下午6:40:37
 * @version 1.1
 */

public class ArticleService {
	
	private Context context;
	private ArticleDAO db ;
	private CategoryDAO categoryDAO;
	public ArticleService(Context context){
		this.context = context;
		db = new ArticleDAO(context);
		categoryDAO = new CategoryDAO(context);
	}
	
	
	//同步每日
	public String syncArticle(){
		L.d("开始同步：每日口语");
		List<ArticleVO> list = new ArrayList<ArticleVO>();
		try{
		DefaultConnection connection = new DefaultConnection();
		long lastAnchor = db.queryAnchor();
		long nextAnchor = new Date().getTime();
		String data = connection.sync_article(lastAnchor+"",nextAnchor+"");
		if(Constants.SYNC_STATUS_FAILURE.equals(data))
			return Constants.SYNC_STATUS_FAILURE;
		if(Constants.SYNC_STATUS_READTIMEOUT.equals(data))
			return Constants.SYNC_STATUS_READTIMEOUT;
		
		if(data==null||"null".equals(data)||"".equals(data))
			return Constants.SYNC_STATUS_NODATA;
		JSONObject obj = new JSONObject(data).getJSONObject("response");
		JSONArray items = obj.getJSONArray("items");
		if(items.length()==0)
			return Constants.SYNC_STATUS_NODATA;
		for(int i=0;i<items.length();i++){
			ArticleVO vo = new ArticleVO();
			JSONObject jo = items.getJSONObject(i);
			vo.setAudio(jo.getString("audio"));
			vo.setCategory(jo.getString("category"));
			vo.setCreatetime(jo.getLong("createtime"));
			vo.setEnglishText(jo.getString("englishtext"));
			vo.setExplain(jo.getString("explain"));
			vo.setTitle(jo.getString("title"));
			vo.setId(jo.getString("id"));
			vo.setType(jo.getString("type"));
			list.add(vo);
		}
		db.sync(list,nextAnchor);
		}catch(Exception e){
			e.printStackTrace();
			return Constants.SYNC_STATUS_FAILURE;
		}
		L.d("每日口语同步结束");
		return Constants.SYNC_STATUS_SUCCESS;
	}
	
	
	public String syncCategory(){
		L.d("开始同步:类别");
		List<CategoryVO> list = new ArrayList<CategoryVO>();
		try{
		DefaultConnection connection = new DefaultConnection();
		long lastAnchor = db.queryAnchor();
		long nextAnchor = new Date().getTime();
		String data = connection.sync_category(lastAnchor+"",nextAnchor+"");
		if(Constants.SYNC_STATUS_FAILURE.equals(data))
			return Constants.SYNC_STATUS_FAILURE;
		if(Constants.SYNC_STATUS_READTIMEOUT.equals(data))
			return Constants.SYNC_STATUS_READTIMEOUT;
		
		if(data==null||"null".equals(data)||"".equals(data))
			return Constants.SYNC_STATUS_NODATA;
		JSONObject obj = new JSONObject(data).getJSONObject("response");
		JSONArray items = obj.getJSONArray("items");
		if(items.length()==0)
			return Constants.SYNC_STATUS_NODATA;
		for(int i=0;i<items.length();i++){
			CategoryVO vo = new CategoryVO();
			JSONObject jo = items.getJSONObject(i);
			vo.setName(jo.getString("name"));
			vo.setId(jo.getString("id"));
			vo.setType(jo.getString("type"));
			list.add(vo);
		}
		categoryDAO.sync(list,nextAnchor);
		}catch(Exception e){
			e.printStackTrace();
			return Constants.SYNC_STATUS_FAILURE;
		}
		L.d("类别同步结束");
		return Constants.SYNC_STATUS_SUCCESS;
	}
	
	//查询
	public List<ArticleVO> load(String page,String count){
		return db.getAll(page, count);
	}
	
	//删除
	public int delete(String id){
		return db.delete(id);
	}
	
	//标记为收藏
	public int mark(String id){
		return db.mark(id);
	}
	
	//取消收藏
	public int unMark(String id){
		return db.unMark(id);
	}
	
	//是否收藏
	public boolean isMark(String id){
		return db.isMark(id);
	}
	
	//查询所有类别
	public List<CategoryVO> findAllCategory(){
		return categoryDAO.findAll();
	}
}

